package com.ruoyi.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.electronicFence.domain.ReceiveEFData;
import com.ruoyi.rsikManage.domain.MonitorPoint;

import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

interface FunctionalInterface {  // 默认访问权限
    void performAction(String action);
}

public class Main2 {
    public static void main(String[] args) {
        // 使用Lambda表达式实现FunctionalInterface
        split();
        OrgInfoUpdate();
    }

    public static void test(){
        List<MonitorPoint> allList=new ArrayList<>();
        MonitorPoint monitorPoint1 = new MonitorPoint();
        MonitorPoint monitorPoint2 = new MonitorPoint();
        monitorPoint1.setName("1_通道_2");
        monitorPoint2.setName("1");
        allList.add(monitorPoint1);
        allList.add(monitorPoint2);
        List<MonitorPoint> list=new ArrayList<>();
        for (
            MonitorPoint monitorPoint : allList) {
            String name = monitorPoint.getName();
            if (name.contains("_通道_")) {
                System.out.println("找到了含有 '_通道_' 的监控点名称: " + name);
            }else {
                list.add(monitorPoint);
                System.out.println(monitorPoint);
            }
        }
    }


    public static void test2(){
        System.out.println("test2");

        String pictureUrls = "[\"1\",\"2\",\"3\"]";
        JSONArray jsonArray = new JSONArray(pictureUrls);
        System.out.println(jsonArray);

        List<String> urlList = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            urlList.add(jsonArray.getString(i));
            System.out.println(urlList.get(i));
        }

        // Convert List<String> back to JSON array
        JSONArray newJsonArray = new JSONArray(urlList);
        String jsonString = newJsonArray.toString();

    }

    /**
     * 异步调用 ping 命令并循环指定次数。
     *
     * @param ipAddress 目标 IP 地址
     * @param iterations 循环次数
     */
    public static void asyncPing(String ipAddress, int iterations) {
        // 创建一个固定大小的线程池
        ExecutorService executor = Executors.newFixedThreadPool(50); // 可根据实际情况调整线程池大小

        // 循环提交任务
        for (int i = 0; i < iterations; i++) {
            CompletableFuture.runAsync(() -> {
                try {
                    ping(ipAddress);
                } catch (IOException e) {
                    System.err.println("Error during ping: " + e.getMessage());
                }
            }, executor);
        }

        // 关闭线程池
        executor.shutdown();
        try {
            // 等待所有任务完成
            executor.awaitTermination(Long.MAX_VALUE, java.util.concurrent.TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("Interrupted while waiting for tasks to complete.");
        }
    }

    /**
     * 执行 ping 命令。
     *
     * @param ipAddress 目标 IP 地址
     * @throws IOException 如果执行过程中发生 I/O 错误
     */
    private static void ping(String ipAddress) throws IOException {
        Process process = Runtime.getRuntime().exec("ping -c 1 " + ipAddress);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        }
    }

    private static void map( ){
        HashMap<Object, Object> map = new HashMap<>();
        ReceiveEFData receiveEFData = new ReceiveEFData();

        String pictureUrl = (String) map.get("pictureUrl");
        if (pictureUrl != null && !pictureUrl.isEmpty()) {
            receiveEFData.setPictureUrl(pictureUrl);
        } else {
            // 可以设置默认值或记录日志等
            // receiveEFData.setPictureUrl("default-url");
            // logger.warn("pictureUrl is missing or empty.");
        }
    }
    private static void split( ){
        String input = "";  // 示例输入
// 检查是否存在至少两个逗号
        if (input.matches(".*,.+,.*")) {
            // 使用正则表达式提取第二个逗号前和第一个逗号之间的内容
            String[] parts = input.split(",", 2);
            if (parts.length > 1) {
                String secondPart = parts[1];
                int secondCommaIndex = secondPart.indexOf(',');
                if (secondCommaIndex != -1) {
                    System.out.println(secondPart.substring(0, secondCommaIndex));
                }
            }
        }else if (input.contains(",")){
            // 提取第一个逗号后面的内容
            String[] parts = input.split(",", 2);
            if (parts.length > 1) {
                System.out.println(parts[1]);

            }
        }else {
            System.out.println(input);
        }



    }


    public static void OrgInfoUpdate()  {
//        String indexAndName = jyService.getIndexAndName();



// 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();

        try {

            String orgName ="{\n" +
                    "    \"error_code\": \"0000000000\",\n" +
                    "    \"message\": \"操作成功\",\n" +
                    "    \"data\": [\n" +
                    "        {\n" +
                    "            \"org_id\": 2,\n" +
                    "            \"parent_id\": 0,\n" +
                    "            \"org_index\": \"10\",\n" +
                    "            \"org_parent_index\": \"0\",\n" +
                    "            \"org_name\": \"本域\",\n" +
                    "            \"region_code\": \"100000\",\n" +
                    "            \"region_code_desc\": null,\n" +
                    "            \"cascaded_id\": null,\n" +
                    "            \"address\": null,\n" +
                    "            \"description\": null,\n" +
                    "            \"type\": \"0\",\n" +
                    "            \"street\": null,\n" +
                    "            \"display_order\": 0,\n" +
                    "            \"deleted\": 0,\n" +
                    "            \"index_path\": \"0/\",\n" +
                    "            \"is_region\": \"true\",\n" +
                    "            \"identification\": null,\n" +
                    "            \"sub_type\": null,\n" +
                    "            \"longitude\": 132.6,\n" +
                    "            \"latitude\": 31.5,\n" +
                    "            \"related_org_index\": null,\n" +
                    "            \"related_org_name\": null,\n" +
                    "            \"owner_to\": null,\n" +
                    "            \"area_type\": null,\n" +
                    "            \"is_owner\": 1,\n" +
                    "            \"child\": [\n" +
                    "                {\n" +
                    "                    \"org_id\": 3,\n" +
                    "                    \"parent_id\": 2,\n" +
                    "                    \"org_index\": \"1000001001\",\n" +
                    "                    \"org_parent_index\": \"10\",\n" +
                    "                    \"org_name\": \"煤矿1\",\n" +
                    "                    \"region_code\": \"100000\",\n" +
                    "                    \"region_code_desc\": null,\n" +
                    "                    \"cascaded_id\": null,\n" +
                    "                    \"address\": null,\n" +
                    "                    \"description\": \"\",\n" +
                    "                    \"type\": \"0\",\n" +
                    "                    \"street\": \"\",\n" +
                    "                    \"display_order\": 0,\n" +
                    "                    \"deleted\": 0,\n" +
                    "                    \"index_path\": \"0/10/\",\n" +
                    "                    \"is_region\": \"false\",\n" +
                    "                    \"identification\": null,\n" +
                    "                    \"sub_type\": null,\n" +
                    "                    \"longitude\": null,\n" +
                    "                    \"latitude\": null,\n" +
                    "                    \"related_org_index\": null,\n" +
                    "                    \"related_org_name\": null,\n" +
                    "                    \"owner_to\": null,\n" +
                    "                    \"area_type\": null,\n" +
                    "                    \"is_owner\": 1,\n" +
                    "                    \"child\": null\n" +
                    "                }\n" +
                    "            ]\n" +
                    "        }\n" +
                    "    ]\n" +
                    "}";
            // 将 JSON 字符串转换为 Map
            Map<String, Object> orgInfoMap = objectMapper.readValue(orgName, Map.class);
            ArrayList<Map<String,Object>> orgInfoList = new ArrayList<>();
            List<Map<String,Object>> data = (List<Map<String,Object>>)orgInfoMap.get("data");
            for (Map<String,Object> org: data){
                HashMap<String, Object> map1 = new HashMap<>();

                Integer org_id = (Integer)org.get("org_id");
                String org_name = (String)org.get("org_name");
                map1.put("org_id",org_id);
                map1.put("org_name",org_name);
                orgInfoList.add(map1);

                List<Map<String,Object>> child = (List<Map<String,Object>>) org.get("child");
                for (Map<String,Object> childOrg :child){

                    HashMap<String, Object> childMap = new HashMap<>();

                    Integer child_org_id = (Integer)childOrg.get("org_id");
                    String child_org_name = (String)childOrg.get("org_name");

                    childMap.put("org_id",child_org_id);
                    childMap.put("org_name",child_org_name);

                    orgInfoList.add(childMap);
                }


                for (Map<String,Object> map :orgInfoList){
                    Integer org_id1 = (Integer)map.get("org_id");
                    String org_name2 = (String)map.get("org_name");
                }
            }


            ArrayList<Map<String, Object>> orgInfoList1 = orgInfoList;
            // 打印转换后的 Map
            System.out.println(orgInfoMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    {
        String orgName ="{\n" +
                "    \"error_code\": \"0000000000\",\n" +
                "    \"message\": \"操作成功\",\n" +
                "    \"data\": [\n" +
                "        {\n" +
                "            \"org_id\": 2,\n" +
                "            \"parent_id\": 0,\n" +
                "            \"org_index\": \"10\",\n" +
                "            \"org_parent_index\": \"0\",\n" +
                "            \"org_name\": \"本域\",\n" +
                "            \"region_code\": \"100000\",\n" +
                "            \"region_code_desc\": null,\n" +
                "            \"cascaded_id\": null,\n" +
                "            \"address\": null,\n" +
                "            \"description\": null,\n" +
                "            \"type\": \"0\",\n" +
                "            \"street\": null,\n" +
                "            \"display_order\": 0,\n" +
                "            \"deleted\": 0,\n" +
                "            \"index_path\": \"0/\",\n" +
                "            \"is_region\": \"true\",\n" +
                "            \"identification\": null,\n" +
                "            \"sub_type\": null,\n" +
                "            \"longitude\": 132.6,\n" +
                "            \"latitude\": 31.5,\n" +
                "            \"related_org_index\": null,\n" +
                "            \"related_org_name\": null,\n" +
                "            \"owner_to\": null,\n" +
                "            \"area_type\": null,\n" +
                "            \"is_owner\": 1,\n" +
                "            \"child\": [\n" +
                "                {\n" +
                "                    \"org_id\": 3,\n" +
                "                    \"parent_id\": 2,\n" +
                "                    \"org_index\": \"1000001001\",\n" +
                "                    \"org_parent_index\": \"10\",\n" +
                "                    \"org_name\": \"煤矿1\",\n" +
                "                    \"region_code\": \"100000\",\n" +
                "                    \"region_code_desc\": null,\n" +
                "                    \"cascaded_id\": null,\n" +
                "                    \"address\": null,\n" +
                "                    \"description\": \"\",\n" +
                "                    \"type\": \"0\",\n" +
                "                    \"street\": \"\",\n" +
                "                    \"display_order\": 0,\n" +
                "                    \"deleted\": 0,\n" +
                "                    \"index_path\": \"0/10/\",\n" +
                "                    \"is_region\": \"false\",\n" +
                "                    \"identification\": null,\n" +
                "                    \"sub_type\": null,\n" +
                "                    \"longitude\": null,\n" +
                "                    \"latitude\": null,\n" +
                "                    \"related_org_index\": null,\n" +
                "                    \"related_org_name\": null,\n" +
                "                    \"owner_to\": null,\n" +
                "                    \"area_type\": null,\n" +
                "                    \"is_owner\": 1,\n" +
                "                    \"child\": null\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";
    }








}
